package com.zhu.storesmanager.views;

import java.util.ArrayList;
import java.util.List;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.SalesInquiryResultActivity;
import com.zhu.storesmanager.SvInquiryModelResultActivity;
import com.zhu.storesmanager.utils.Actions;
import com.zhu.storesmanager.utils.DataUtils;
import com.zhu.storesmanager.utils.Log;
import com.zhu.storesmanager.utils.SalesData;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

public class SampleModelsWallView extends LinearLayout implements OnClickListener{

    public static final int TYPE_THEME = 0;  //主题形象墙
    public static final int TYPE_TIME = 1; //流光益彩墙
    public static final int TYPE_OTHER = 2; //其他
    public static final int TYPE_NEEDADDRES = 3; //补充货源
    
    private TextView mTitleView = null;
    private int mCurType = TYPE_THEME;
    private LinearLayout mListBody = null; //列表内容集合
    private boolean isSelected = true;//默认选中
    private Activity mActivity = null;
    private List<String> mModelList = new ArrayList<String>();
    private boolean canDataDuplication = true; //是否允许型号数据重复

    /**
     * 构造函数
     */
    public SampleModelsWallView(Context context) {
        this(context, null);
    }

    public SampleModelsWallView(Context context, AttributeSet attrs) {
        this(context, attrs ,0);
    }

    public SampleModelsWallView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    /**
     * 设置界面
     * @param type 出样类型
     */
    public void setupViews(Activity activity, int type){
        mActivity = activity;
        mCurType = type;
        mListBody = (LinearLayout)findViewById(R.id.listbody);
        mTitleView = (TextView)findViewById(R.id.title);
        findViewById(R.id.sample_search_button).setOnClickListener(this); //搜索
        mTitleView.setText(getTitleId(type));
        if(type == TYPE_TIME || type == TYPE_NEEDADDRES){ //流光title设置checkbox
            mTitleView.setOnClickListener(this);
            updateCheckStatus();
        }
        if(type == TYPE_NEEDADDRES){//补充货源型号数据不能重复
        	canDataDuplication = false;
        }
        if(DataUtils.mUserData.isAdmin()){
        	findViewById(R.id.sample_search).setVisibility(View.GONE);
        	findViewById(R.id.sample_modellist).setVisibility(View.VISIBLE);
        	mTitleView.setEnabled(false);
        }
    }

    /**
     * 设置显示lable
     * @param text 文本
     */
    public void setLabelText(String text){
        mTitleView.setText(text);
    }

    /**
     * 更新checkbox选择状态
     */
    private void updateCheckStatus(){
        if(isSelected){
            mTitleView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.select_on_red), null, null, null);
        }else{
            mTitleView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.select_off), null, null, null);
        }
    }

    /**
     * 根据类型获取标题ID
     * @param type 出样类型
     */
    private int getTitleId(int type){
        if(type == TYPE_THEME){
            return R.string.sample_theme;
        }else if(type == TYPE_TIME){
            return R.string.sample_time;
        }else if(type == TYPE_OTHER){
            return R.string.sample_other;
        }else if(type == TYPE_NEEDADDRES){
        	return R.string.sales_addres;
        }
        return -1;
    }

    /**
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v != null){
            switch(v.getId()){
                case R.id.title: //点击checkboxx
                    isSelected = !isSelected;
                    updateCheckStatus();
                    break;
                case R.id.sample_search_button: //搜索添加型号
                    gotoSearch();
                    break;
                case R.id.model_delete: //删除
                	deleteModel((String)v.getTag());
                	break;
            }
        }
    }


    /**
     * 查询
     */
    private void gotoSearch(){
        String model = ((EditText)findViewById(R.id.sample_search_model)).getText().toString(); //获取型号输入框数据
        String pfl = ((EditText)findViewById(R.id.sample_search_pel)).getText().toString(); //获取PEL输入框数据
        //检查内容是合法
        if(DataUtils.checkInputSearch(model, pfl)){
            //跳转查询结果界面
            Intent intent = new Intent();
            intent.setClass(getContext(), SvInquiryModelResultActivity.class);
            intent.putExtra(SalesInquiryResultActivity.MODEL, model); //传递查询条件 model
            intent.putExtra(SalesInquiryResultActivity.PFL, pfl); //传递查询条件 pel
            mActivity.startActivityForResult(intent, mCurType);
            return;
        }
        Actions.toastShort(mActivity, getContext().getString(R.string.search_fail));
    }

    /**
     * 新增型号
     * @param models新增型号集合
     */
    public void addModel(String models){
        if(models != null){
            Log.d("models="+models);
            String[] modelArr = models.split(SalesData.SPLIT_MORE);
            Log.d("modellenghs="+modelArr.length);
            for(String model : modelArr){ //遍历所有新增型号集合
            	if(!canDataDuplication){ //不允许重复数据
            		if(mModelList.contains(model)){ //检查型号是是否存在
            			continue;
            		}
            	}
                mModelList.add(model);
                View view = getItemView(model, -1);
                mListBody.addView(view);
            }
        }
    }

    /**
     * 新增型号
     * @param models 新增型号集合
     */
    public void addModel(List<String> models){
        if(models != null){
        	int index = 1;
            for(String model : models){ //遍历所有新增型号集合
            	if(!canDataDuplication){ //不允许重复数据
            		if(mModelList.contains(model)){ //检查型号是是否存在
            			continue;
            		}
            	}
                mModelList.add(model);
                View view = getItemView(model, index);
                index++;
                mListBody.addView(view);
            }
        }
    }

    /**
     * 获取型号列表项界面
     * @param model 型号
     * @return view 界面
     */
    private View getItemView(String model, int index){
    	if(DataUtils.mUserData.isAdmin()){
    		View row = LayoutInflater.from(mActivity).inflate(R.layout.storelist_item, null);
            ((TextView) row.findViewById(R.id.store_xuhao)).setText(index  + "");
            ((TextView) row.findViewById(R.id.store_name)).setText(model);
            return row;
    	}else{
			View view = LayoutInflater.from(mActivity).inflate(
					R.layout.svsample_model_item, null);
			((TextView) view.findViewById(R.id.model_name)).setText(model);
			View button = view.findViewById(R.id.model_delete);
			button.setVisibility(View.VISIBLE);
			button.setTag(model);
			button.setOnClickListener(this);
			return view;
    	}
       
    }

    /**
     * 获取型号数量
     */
    public int getModelCount(){
    	if(isSelected){
    		return mModelList.size();
    	}
    	return 0;
    }

    /**
     * 获取型号数据保存至数据库
     * @return models 型号数据 多条数据以";"分割
     */
    public String getModels(){
    	if(isSelected){
    		if(mModelList.size() > 0){
    			String models = "";
    			for(String model : mModelList){
    				models += model + SalesData.SPLIT_MORE;
    			}
    			return models;
    		}
    	}
    	return null;
    }

    /**
     * 删除选择的型号
     */
    private void deleteModel(String model){
    	int index = mModelList.indexOf(model);
    	mModelList.remove(index);
    	mListBody.removeViewAt(index);
    }

    /**
     * 检测数据是否填写完整
     * @param isShow 是否显示
     * @return true 完整 否则反之
     */
    public boolean checkDataCompelete(boolean isShow){
    	if(isSelected && isShow){
    		if(mModelList != null && mModelList.size() > 0){
    			return true;
    		}else{
    			return false;
    		}
    	}
    	return true;
    }

    /**
     * 是否选中
     */
    public boolean isSelected(){
    	return isSelected;
    }

    /**
     * 设置选中状态
     * @param selected 是否选中
     */
    public void setSelectedStatus(boolean selected){
    	isSelected = selected;
    	updateCheckStatus();
    }
}
